import debounce from "@/uview-ui/libs/function/debounce.js"
import {
	Set_BaseStorage
} from "../../common";
//获取当前页对象和url
function getCurrentPageUrl() {
	var pages = getCurrentPages();
	var currentPage = pages[pages.length - 1];
	var url = currentPage.route;
	return {
		currentPage,
		url
	}
}

//封装wx.request()
function ajaxRequest(method, url, data) {

	let header = {
		'content-type': 'application/x-www-form-urlencoded',
	}
	if (uni.getStorageSync('token')) {
		header = {
			'content-type': 'application/x-www-form-urlencoded',
			'token': uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
		}
	}
	var promise = new Promise((resolve, reject) => {
		var that = this;
		uni.request({
			url: getApp().globalData.head2 + url,
			data,
			method,
			header,
			success(res) {

				if (res.statusCode != 200) {
					console.log("请求出错statusCode" + res.statusCode, url);
					uni.showToast({
						title: res.statusCode,
						icon: 'none'
					});
				}

				if (res.data.code == -100) {
						uni.clearStorage()

					if (!uni.getStorageSync('isRefresh')) {
						refreshtoken().then(() => {
							uni.setStorageSync('refreshSuccess', true)
							ajaxRequest(method, url, data).then(res => {
								resolve(res)
							})
						}).catch(() => {
							uni.setStorageSync('refreshSuccess', false)
							uni.showToast({
								title: '登录失效，请重新登录',
								icon: 'none'
							});
							uni.removeStorageSync('isLogin')
							uni.removeStorageSync('token')
							setTimeout(() => {

								uni.navigateTo({
									url: '/pages/login/login'
								})
							}, 2000)
						})

						uni.setStorageSync('isRefresh', true)
						setTimeout(() => {
							uni.setStorageSync('isRefresh', false)
						}, 20000)
					} else {
						uni.showLoading({
							title: '重新登录中'
						})
						setTimeout(() => {
							if (uni.getStorageSync('refreshSuccess')) {
								ajaxRequest(method, url, data).then(res => {
									resolve(res)
								})
							} else {
								reject()
							}

							uni.hideLoading()
						}, 5000)
					}




				} else if(res.data.code == -1){
						reject();
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
				} else {
					if (uni.getStorageSync('token')) { //处理更新版本字典词库
						var edition = res.data.edition

						var localedition = uni.getStorageSync('edition')
						if (localedition) {
							if (edition.dic != localedition.dic) {
								getApp().getdic()

							}
							if (edition.unit != localedition.unit) {
								getApp().getunit()
							}
							if (edition.user != localedition.user) {
								getApp().getuser()
							}

						} else {
							getApp().getdic()
							getApp().getunit()
							getApp().getuser()


						}

						uni.setStorageSync('edition', edition)
					}

					if (res.data.code == 0 || res.data.code == 10) {
						resolve(res.data);

					} else {

						reject();
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}


				}
			},
			fail(res) {
				console.log('err', JSON.stringify(res))
				uni.showToast({
					content: JSON.stringify(res),
					icon: 'none'

				})
				reject(res);
			}
		})
	});
	return promise;
}

function refreshtoken() {

	var promise = new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success: (loginRes) => {

				var code = loginRes.code;
				uni.request({
					url: getApp().globalData.head2 + '/firstLoginWeChat',
					data: {
						code
					},
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					},
					success: (res => {
						if (res.data.code == 0) {
							Set_BaseStorage(res.data)
							resolve(true)
						} else {
							reject()
						}
					})
				})


			}
		})
	});
	return promise;
}




function gotologin() {

	uni.showToast({
		title: '登录失效，请重新登录',
		icon: 'none'
	});
	uni.removeStorageSync('isLogin')
	uni.removeStorageSync('token')
	setTimeout(() => {

		uni.navigateTo({
			url: '/pages/login/login'
		})
	}, 1500)
}
//封装post请求
function ajaxPost(url, data) {
	return ajaxRequest('POST', url, data)
}

//封装get请求
function ajaxGet(url, data) {
	return ajaxRequest('GET', url, data)
}

module.exports = {
	getCurrentPageUrl,
	ajaxPost,
	ajaxGet
}
